package util

import (
	"bytes"
	"gpt-api/framework/log"
	"io/ioutil"
	"net/http"
	UrL "net/url"
	"strconv"
)

func TUIA(AoID string, SubType int, ip string, ua string, platformType int) {
	var data string
	var advertKey string

	url := "https://activity.tuia.cn/log/effect/v2"
	// 将 form-data 传参 封装成一个字符串
	Type := 8
	subtype := strconv.Itoa(SubType)
	types := strconv.Itoa(Type)
	UA := UrL.QueryEscape(ua)

	if platformType == 6 {
		advertKey = "E90D11D66975AB4AAB9D4C2C49E406E4"
	}
	if platformType == 7 {
		advertKey = "8F64249669945F3EFFB7D7AC1403078C"
	}
	if platformType == 8 {
		advertKey = "E90D11D66975AB4AAB9D4C2C49E406E4"
	}
	if platformType == 11 {
		advertKey = "773ADA001A6A6B020B6F30A21719A574"
	}
	//advertKey := "8F64249669945F3EFFB7D7AC1403078C"
	if SubType == 0 {
		data = "a_oId=" + AoID + "&" + "advertKey=" + advertKey +
			"&" + "subType=" + subtype + "&" + "type=" + types +
			"&" + "ip=" + ip + "&" + "ua=" + UA
	} else {
		data = "a_oId=" + AoID + "&" + "advertKey=" + advertKey + "&" + "subType=" + subtype +
			"&" + "ip=" + ip + "&" + "ua=" + UA

	}
	// 将字符串转换成一个字节数组
	payload := []byte(data)
	// 使用 bytes.NewReader 将字节数组转换成一个 reader
	req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
	if err != nil {
		log.Error("TuiA请求失败", log.Var("err", err))
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Content-Length", "100")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Error("TuiA请求失败", log.Var("err", err))
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Error("TuiA请求失败", log.Var("err", err))

	}
	log.Info("TuiA请求成功", log.Var("body", string(body)))
}
